/**
 * Predefined variables
 * Name = hallScence
 * DateTime = Mon Jan 17 2022 13:06:46 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = hallScence.ts
 * gitee： https://gitee.com/coyeking
 */
 
import { _decorator, Component, Node,Sprite,Label,sys, Prefab,instantiate, director } from 'cc';
const { ccclass, property } = _decorator;
import { audioManager } from '../framework/audioManager';
import { userGlobal } from '../global/userGlobal';
import { resourceUtil } from '../framework/resourceUtil';
import { helper } from '../utils/helper';
import { uiManager } from '../framework/uiManager';
import { constant } from '../define/constant';
import { notifyEvent } from '../framework/notifyEvent';
import { platformHelper } from '../utils/platformHelper';
import { socketManager } from '../framework/socketManager';
import { ModeluType,Room_Pay_Type,Room_Type } from '../define/ccenum';
import { httpApi } from '../api/httpApi';
 
@ccclass('hallScence')
export class hallScence extends Component {
    @property(Sprite)
    public ui_hallBgSprite: Sprite = null;
    @property(Sprite)
    public ui_userhead: Sprite = null;
    @property(Label)
    public ui_username: Label = null;
    @property(Label)
    public ui_userid: Label = null;
    @property(Label)
    public ui_usergold: Label = null;

    onLoad () {
        this.initUI();
        notifyEvent.on(constant.NOTIFY_EVENT.USER_TOKEN,this.updateHallBg,this);

        socketManager.instance.on('login_finished',(res)=>{
            if (res.code != 1){
                uiManager.showToast(res.msg,ModeluType.FAIL);
            }
            // director.loadScene('gamescence');
        });

    };
    
    onDestroy () {
        notifyEvent.off(constant.NOTIFY_EVENT.USER_TOKEN,this.updateHallBg,this);
    };
	
    initUI(){
        audioManager.instance.playMusic('hall_bg_music');
        this.updataUserInfo();
        this.updateHallBg();
    }

    updataUserInfo(){
        const info = userGlobal.instance.getUserInfo();
        this.ui_username.string = helper.convertNickname(info.nickname);
        this.ui_userid.string = 'ID: ' + info.id;
        this.ui_usergold.string = info.money;
        resourceUtil.loadRemoteImg(info.avatar,this.ui_userhead);
    }

    /**
     * 按钮点击事件
     * @param event 
     * @param customEventData 
     */
	private onClick (event: Event, customEventData: string) {
        audioManager.instance.playSound();
        if (customEventData == 'create'){
            this.testfunc();
        }else if(customEventData == 'setting'){
            uiManager.createLayer('prefab/home/SettingLayer','SettingLayer');
        }else if(customEventData == 'head'){
            uiManager.createLayer('prefab/home/UserLayer','UserLayer');
        }else if(customEventData == 'shop'){
            if (sys.isBrowser && userGlobal.instance.getSystemInfo().h5_ispay == 'true'){
                return uiManager.createLayer('prefab/home/ShopLayer','ShopLayer');
            }else if(!sys.isBrowser && userGlobal.instance.getSystemInfo().app_ispay == 'true'){
                return uiManager.createLayer('prefab/home/ShopLayer','ShopLayer');
            }
            uiManager.showToast('该平台已关闭商城功能');
        }
    }

    updateHallBg(){
        let hallBg = sys.localStorage.getItem(constant.LOCAL_STORAGE.hallBaindex);
        if (!hallBg){
            hallBg = 'hall__bg0';
            sys.localStorage.setItem(constant.LOCAL_STORAGE.hallBaindex,hallBg)
        }

        let img = `texture/hall/bg/${hallBg}`;
        resourceUtil.loadSpriteFrameRes(img,this.ui_hallBgSprite);
    }

    testfunc(){
        socketManager.instance.connect('127.0.0.1:9000',function(){
            socketManager.instance.emit('login',userGlobal.instance.getUserInfo());
        });
        var data = {
            game_kind: 'ddz',
            room_type:Room_Type.NORMAL,
            config : {
                min_score:6,
                balance:1,
                room_pay_type:Room_Pay_Type.HOUSE_OWNER,
            }
        }
        httpApi.createRoom(data,(result)=>{
            
        },(err)=>{

        });
    }
}

